#ifndef IMAGEPROCESSOR_H
#define IMAGEPROCESSOR_H

#include <QObject>
#include <QByteArray>
#include <QMutex>
#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>

class ImageProcessor : public QObject
{
    Q_OBJECT
public:
    explicit ImageProcessor(QObject *parent = nullptr);
    ~ImageProcessor() override;
    
    bool initEncoder(int width, int height, int fps = 30);
    
signals:
    void frameEncoded(const QByteArray &data);
    void errorOccurred(const QString &errorString);
    
public slots:
    void processImageData(const QByteArray &data);
    
private:
    AVCodec *m_codec;
    AVCodecContext *m_codecCtx;
    AVFrame *m_frame;
    AVPacket *m_packet;
    QMutex m_mutex;
    int m_frameCount;
    
    bool convertToYUV420P(const QByteArray &grayData, AVFrame *frame);
    bool encodeFrame(AVFrame *frame, QByteArray &outputData);
};

#endif // IMAGEPROCESSOR_H
